// import {zipObject} from '@/utils/lodash.min.js'
import lodash from '@/utils/lodash.js'

export default class Model {
   /**
	* 属性赋值
	* @param {Any} values
	*/
	setAttributes (values) {
		const _this = this
		const attributeMap = new Map()
		const attributes = this.attributes()
		for (const index in attributes) {
			attributeMap.set(attributes[index], index)
		}
		for (const key in values) {
			if (values.hasOwnProperty(key) && attributeMap.get(key)) {
				const { getType } = this
				switch (getType(_this[key])) {
					case 'boolean':
						_this[key] = !!values[key]
					break
					case 'string':
						_this[key] = values[key] ? values[key] : _this[key]
					break
					case 'object':
						_this[key] = getType(values[key]) === 'object' ? values[key] : _this[key]
					break
					case 'array':
						_this[key] = getType(values[key]) === 'array' ? values[key] : _this[key]
					break
					case 'number':
						_this[key] = values[key] ? Number(values[key]) : _this[key]
					break
					default:
						_this[key] = values[key]
				}
			}
		}
		return _this
	}

  /**
   *
   * @param fields
   * @param expand
   * @param onlyValueKey
   */
	toObject (fields=[], expand=[]) {
		const data = {}
		const _this = this
		let resolveFields = _this.resolveFields(
			fields.length === 0 ? null : lodash.zipObject(fields, fields),
			expand.length === 0 ? null : lodash.zipObject(expand, expand)
		)
		for (const field in fields) {
			const type = typeof fields[field]
			if (type === 'string') {
				data[field] = _this[fields[field]]
			} else if (type === 'function') {
				const funData = fields[field].apply()
				funData !== null && (data[field] = funData)
			}
		}
		for (let field in resolveFields) {
			if (resolveFields.hasOwnProperty(field)) {
				let type = typeof resolveFields[field]
				if (type === 'string') {
					if(_this.hasOwnProperty(resolveFields[field]) && typeof _this[resolveFields[field]] === 'number' && isNaN(_this[resolveFields[field]])){
						continue
					}
					data[field] = _this[resolveFields[field]]
				} else if (type === 'function') {
					let funData = resolveFields[field].apply()
					funData !== null && (data[field] = funData)
				}
			}
		}
		return data
	}

   /**
	* @param {Object} fields
	*/
	resolveFields(fields) {
		let result = {}
		let modelFields = this.fields()
		for (let field in modelFields) {
			if (modelFields.hasOwnProperty(field) && fields === null || fields.hasOwnProperty(field)) {
				result[field] = modelFields[field]
			}
		}
		return result
	}

   /**
	* 写入数据 zipObject(属性名, 属性值)
	*/
	fields () {
		const fields = this.attributes()
		return lodash.zipObject(fields, fields)
	}

   /**
	* 获取 key 值
	*/
	attributes () {
		const _this = this
		const names = []
		names.push(...Object.keys(_this))
		return names
	}

   /**
	* 批量删除
	* @param {Object} fields 需要处理的对象
	* @param {String} delKeys 需要删除的 key 值
	*/
	delFields (fields, delKeys) {
		for (const key of delKeys) {
			delete fields[key]
		}
	}

   /**
	* 判断类型
	* @param {String} val
	*/
	getType (val) {
		let type = Object.prototype.toString.apply(val)
		switch (type) {
			case '[object String]':
				type = 'string'
			break
			case '[object Number]':
				type = 'number'
			break
			case '[object Boolean]':
				type = 'boolean'
			break
			case '[object Undefined]':
				type = 'undefined'
			break
			case '[object Null]':
				type = 'null'
			break
			case '[object Object]':
				type = 'object'
			break
			case '[object Array]':
				type = 'array'
			break
			case '[object Function]':
				type = 'function'
			break
		}
		return type
	}
}